/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.scs.util;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 *
 * @author Wisnu Wardoyo
 */
public class MySQLUtil {

    private static Connection connection;

    public static Connection getConnection(Properties properties) throws SQLException {

        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL(properties.getProperty("hibernate.connection.url"));
        dataSource.setUser(properties.getProperty("hibernate.connection.username"));
        dataSource.setPassword(properties.getProperty("hibernate.connection.password"));

        connection = dataSource.getConnection();

        return connection;
    }

    public static void createDatabase(Properties properties) throws SQLException {
        if (connection == null) {
            properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/");
            connection = getConnection(properties);
        }
        Statement statement = connection.createStatement();
        statement.executeUpdate("CREATE DATABASE `sparepart`");
        statement.close();
    }
}
